package com.wef.system.service.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;

import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.gfa4j.web.SessionManager;
import com.wef.system.entity.MenuGroup;
import com.wef.system.entity.MenuItem;
import com.wef.system.entity.Module;
import com.wef.system.entity.ModuleTitle;
import com.wef.system.entity.Role;
import com.wef.system.entity.RoleMenuItem;
import com.wef.system.entity.User;
import com.wef.system.entity.ViewButton;
import com.wef.system.entity.domain.MenuItemDomain;
import com.wef.system.entity.domain.ModuleDomain;
import com.wef.system.entity.domain.ViewButtonDomain;
import com.wef.system.service.AuthFunctionService;
import com.wef.system.service.MenuGroupService;
import com.wef.system.service.MenuGroupTitleService;
import com.wef.system.service.MenuItemService;
import com.wef.system.service.MenuItemTitleService;
import com.wef.system.service.ModuleService;
import com.wef.system.service.ModuleTitleService;

public class AuthFunctionServiceImpl extends BaseServiceImpl implements AuthFunctionService {

	private ModuleService moduleService;
	private MenuGroupService menuGroupService;
	private MenuItemService menuItemService;
	private ModuleTitleService moduleTitleService;
	private MenuItemTitleService menuItemTitleService;
	private MenuGroupTitleService menuGroupTitleService;

	public MenuGroupTitleService getMenuGroupTitleService() {
		return menuGroupTitleService;
	}

	public void setMenuGroupTitleService(MenuGroupTitleService menuGroupTitleService) {
		this.menuGroupTitleService = menuGroupTitleService;
	}

	public MenuItemService getMenuItemService() {
		return menuItemService;
	}

	public void setMenuItemService(MenuItemService menuItemService) {
		this.menuItemService = menuItemService;
	}

	public MenuItemTitleService getMenuItemTitleService() {
		return menuItemTitleService;
	}

	public void setMenuItemTitleService(MenuItemTitleService menuItemTitleService) {
		this.menuItemTitleService = menuItemTitleService;
	}

	public ModuleTitleService getModuleTitleService() {
		return moduleTitleService;
	}

	public void setModuleTitleService(ModuleTitleService moduleTitleService) {
		this.moduleTitleService = moduleTitleService;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public MenuGroupService getMenuGroupService() {
		return menuGroupService;
	}

	public void setMenuGroupService(MenuGroupService menuGroupService) {
		this.menuGroupService = menuGroupService;
	}

	public MenuItemService getmenuItemService() {
		return menuItemService;
	}

	public void setmenuItemService(MenuItemService menuItemService) {
		this.menuItemService = menuItemService;
	}

	public List<Module> loadAllModule() {
		List<Module> moduleList = this.loadAll(Module.class);
		for (Module module : moduleList) {
			Hibernate.initialize(module.getModuleTitles());
			Set<MenuGroup> menuGroupList = module.getMenuGroups();
			for (MenuGroup menuGroup : menuGroupList) {
				Hibernate.initialize(menuGroup.getMenuGroupTitles());
				Set<MenuItem> menuItemList = menuGroup.getMenuItems();
				for (MenuItem menuItem : menuItemList) {
					Hibernate.initialize(menuItem.getMenuItemTitles());
				}
			}
		}
		return moduleList;
	}

	public void removeModuleTitles(Set<ModuleTitle> moduleTitles) {
		moduleTitleService.removeModuleTitles(moduleTitles);
	}

	public MenuGroup saveOrUpdateMenuGroup(MenuGroup menuGroup, String parentId) {
		if (menuGroup.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			this.validateSaveMenuGroup(menuGroup);
			// liucy 2011.3.18 start
			return (MenuGroup) menuGroupService.addMenuGroupByParentId(menuGroup, parentId);
			// liucy 2011.3.18 end
		} else if (menuGroup.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			this.validateSaveMenuGroup(menuGroup);
			// liucy 2011.2.9 start
			return (MenuGroup) menuGroupService.updateMenuGroupById(menuGroup);
			// liucy 2011.2.9 end
		} else if (menuGroup.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			menuGroupService.removeMenuGroup(menuGroup);
			return null;
		}
		return menuGroup;
	}

	public MenuItem saveOrUpdateMenuItem(MenuItem menuItem, String parentId) {
		User user = (User) SessionManager.getUserDetail().getObject();
		if (menuItem.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			this.validateSaveMenuItem(menuItem);
			menuItem.setIsDialog(false);
			// liucy start
			MenuItem mi = menuItemService.addMenuItemByParentId(menuItem, parentId);
			// liucy end
			Role role = (Role) findByProperty(Role.class, "roleName", "admin").get(0);
			if (role != null) {
				RoleMenuItem rmi = new RoleMenuItem();
				rmi.setMenuItem(menuItem);
				rmi.setRole(role);
				rmi.setCreatedByUser(user.getUserCode());
				super.saveObject(rmi);
			}
			return mi;
		} else if (menuItem.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			this.validateSaveMenuItem(menuItem);
			// liucy 2011.2.9 start
			return (MenuItem) menuItemService.updateMenuItemById(menuItem);
			// liucy 2011.2.9 end
		} else if (menuItem.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			// deleteObjectByProperty(ViewButton.class, ViewButtonDomain.menuItem, menuItem);
			// deleteObjectByProperty(RoleMenuItem.class, RoleMenuItemDomain.menuItem, menuItem);
			menuItemService.deleteMenuItem(menuItem);
			return null;
		}
		return menuItem;
	}

	public Module saveOrUpdateModule(Module module) {
		Module reModule = null;
		if (module.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			this.validateSaveModule(module);
			return (Module) moduleService.addModule(module);
		} else if (module.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			this.validateSaveModule(module);
			reModule = (Module) moduleService.updateModule(module);
			return reModule;
		} else if (module.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			moduleService.removeModule(module);
			return null;
		}
		return module;
	}
	
	public ViewButton saveOrUpdateViewButton(ViewButton viewButton) {
		if (viewButton.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {	
			MenuItem menuItem = (MenuItem) (this.findByProperty(MenuItem.class, MenuItemDomain.menuItemId, viewButton.getMenuItem().getMenuItemId()).get(0));
			viewButton.setMenuItem(menuItem);
			this.validateSaveViewButton(viewButton);
			return (ViewButton) this.saveObject(viewButton);
		} else if (viewButton.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			MenuItem menuItem = (MenuItem) (this.findByProperty(MenuItem.class, MenuItemDomain.menuItemId, viewButton.getMenuItem().getMenuItemId()).get(0));
			viewButton.setMenuItem(menuItem);
			this.validateSaveViewButton(viewButton);
			return (ViewButton) this.updateObject(viewButton);
		} else if (viewButton.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.validateDelete(viewButton);
			super.deleteObject(viewButton);
			return null;
		}
		return viewButton;
	}

	private void validateSaveModule(Module module) {
		this.validateUnique(module, ModuleDomain.moduleCode, module.getModuleCode());
	}

	private void validateSaveMenuGroup(MenuGroup mg) {
		// this.validateUnique(mg, MenuGroupDomain.menuGrpTitleKey, mg.getMenuGrpTitleKey());
	}

	private void validateSaveMenuItem(MenuItem mi) {
		// this.validateUnique(mi, MenuItemDomain.menuItemTitleKey, mi.getMenuItemTitleKey());
	}

	private void validateSaveViewButton(ViewButton vb) {
		// this.validateUnique(vb, ViewButtonDomain.btnName, vb.getBtnName());
	}

}
